<?php

require_once('lib.php');
//gatekeeper();

$method = get_input('method');

switch ($method) {
	/**
	 * Outputs the metadata for the emMap with guid=$guid
	 * @param integer $guid The GUID for an emMap
	 */	
	case 'map.getInfo':
		emShow( array('map'=>Map::getInfo( get_input('guid') ) ) );
		break;
	case 'map.createNew':
		emShow( Map::createNew() );
		break;
	case 'map.save':
		emShow(emUpdateMapAsRequestedFromAjax());
		break;
	case 'map.delete':
		emDeleteMap(get_input( 'guid',  false ) );
		break;	
	case 'dialog.map.getPublic':
		emShowPublicMapsList(get_input('guid'));
		break;
	case 'dialog.visibleMaps':
		emShowVisibleMapsList(get_input('guid'));
		break;
	case 'dialog.map.userowned':
		emShowOwnMapsList(get_input('guid'));
		break;	
	case 'dialog.map.getInfo':
		emShowMapMetadata(get_input('guid'));
		break;
	case 'dialog.map.edit':
		emShowEditMapForm(get_input('guid', false));
		break;
	case 'dialog.auth.login':
		emShowLoginForm();
		break;
	case 'dialog.auth.register':
		emShowRegisterForm();
		break;
	case 'dialog.auth.sessioninfo':
		emShowSessionInfo();
		break;
	case 'auth.isLoggedIn':
		echo json_encode(array('response' => isloggedin()));
		break;
	case 'auth.login':
		emLogin();
		break;
	case 'auth.logout':
		emLogout();
		break;
	case 'map.canEdit':
		if ($map = emGetMap(get_input('guid'))) {
			echo json_encode( array( "response"=> $map->canEdit() ) );
		} else {
			echo json_encode( array( "response"=> isloggedin() ) );
		}
		break;
	case 'feature.search':
		// la consulta viene como parámetro q
		$q = get_input( "q" );
		// si existe pg_query.php resuelvo el método
		//con la función de búsqueda de ese archivo
		if (file_exists( "pg_query.php" ) ) {
			require_once( "pg_query.php");
			emShow(IGN_searchFeature($q));
		}
		break;
	case 'dialog.feature.search':
		$q = get_input( "q" );
		// si existe pg_query.php resuelvo el método
		// con la función de búsqueda de ese archivo
		if ( file_exists( "pg_query.php" ) ) {
			require_once( "pg_query.php" );
			emShow( IGN_searchFeature( $q, true ) );
		}
		break;
	default:
		echo "oops";
}
?>
